package com.example.springcloudwinter.service;

import com.example.springcloudwinter.bo.AddressBo;
import com.example.springcloudwinter.dto.ResultEntity;
import com.example.springcloudwinter.mapper.DeliveryAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class DeliveryAddressService {
    
    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;
    
    public ResultEntity<?> changeDeliveryAddress (String userId, int addressId, String name, String address, String phoneNumber, int sex) {
        ResultEntity<?> result = new ResultEntity<>();
        deliveryAddressMapper.changeAddress(userId, sex, phoneNumber, address, name, addressId);
        result.setCode(200);
        return result;
    }
    
    public ResultEntity<?> getDeliveryAddress (String userId) {
        ResultEntity<List<?>> result = new ResultEntity<>();
        List<AddressBo> addressList = null;
        try {
            addressList = deliveryAddressMapper.getAddressByUserId(userId);
            result.setCode(200);
            result.setDetail(addressList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public ResultEntity<?> saveDeliveryAddress (String userId, String name, String address, String phoneNumber, int sex) {
        ResultEntity<?> result = new ResultEntity<>();
        try {
            if (deliveryAddressMapper.saveDeliveryAddress(userId, name, address, phoneNumber, sex) <= 0) return result;
            else result.setCode(200);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public ResultEntity<?> getDeliveryAddressByAddressId (int addressId, String userId) {
        ResultEntity<AddressBo> result = new ResultEntity<>();
        AddressBo address;
        try {
            address = deliveryAddressMapper.getDeliveryAddressByAddressId(addressId, userId);
            result.setDetail(address).setCode(200);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public ResultEntity<?> removeDeliveryAddress (int addressId, String userId) {
        ResultEntity<?> result = new ResultEntity<>();
        try {
            if (deliveryAddressMapper.removeDeliveryAddress(addressId, userId) <= 0) return result;
            else result.setCode(200);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}


